<?php

require_once 'modules/content/models/Content.php';
require_once 'modules/content/models/ContentCategory.php';

class content_IndexController extends Nine_Controller_Action
{
	public function indexAction()
	{
		$objContent = new Models_Content();
		$objContentCategory = new Models_ContentCategory();
		
		/**
		 * Get category
		 */
		$categoryGid = $this->_getParam('cid',false);
		if (false == $categoryGid) {
			$this->_redirectToNotFoundPage();
		}
		$catName = $objContentCategory->getCategoryWithParent($categoryGid);
//		echo "<pre>";print_r($catName);die;
		$numRowPerPage = Nine_Registry::getConfig("newsNumberRowPerPage");
		$currentPage = $this->_getParam("page",1);
//		echo "<pre>";print_r($numRowPerPage);die;
		/**
		 * Get latest news
		 */
		$latestNews = $objContent->getLatestContentByCategory( $categoryGid );
//		echo "<pre>";print_r($latestNews);die;
		if (null == $latestNews) {
			$this->_redirectToNotFoundPage();
		}
		
		$cat = @reset($objContentCategory->getByColumns(array('content_category_gid=?' => $latestNews['content_category_gid']))->toArray());
		if (null == $cat) {
			$this->_redirectToNotFoundPage();
		}
		
		$latestNews['category_name'] = $cat['name'];
		$latestNews['main_title'] = $latestNews['title'];
		$latestNews['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($latestNews['title'])),30);
        $latestNews['full_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($latestNews['full_text'])), 145);
        $latestNews['url'] = Nine_Route::_("content/index/detail/id/{$latestNews['content_gid']}",array('alias' => $latestNews['alias']));
		$tmp = explode('||', $latestNews['images']);
		$latestNews['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 244,191,null,true);
		if(null!=$latestNews['intro_text']){
        		$latestNews['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($latestNews['intro_text'])), 790);
        	}
        else{
        		$latestNews['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($latestNews['full_text'])), 790);
        }
		
		
		
		/**
		 * Get all contents
		 */
		$condition = array(
					'exclude_content_gids'	=>	$latestNews['content_gid']
					);
		
		
		$allContents = $objContent->getAllEnabledContentByCategory($categoryGid, $condition, array('sorting ASC','content_id DESC'),
                                                   $numRowPerPage,($currentPage - 1) * $numRowPerPage
                                                   );
		/**
		 * Modified all contents
		 */
		foreach($allContents as &$content){
			$content['main_title']  = $content['title'] ;
			$content['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($content['title'])),50);
        	$content['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($content['intro_text'])), 130); 
        	if(null!=$content['intro_text']){
        		$content['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($content['intro_text'])), 130); 
        	}
       		 else{
        		$content['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($content['full_text'])), 130); 
        	}
		       	
        	$content['url'] = Nine_Route::_("content/index/detail/id/{$content['content_gid']}",array('alias' => $content['alias']));
        	$tmp = explode('||', $content['images']);
//			$content['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 244, 191);
			$content['main_image']= Nine_Function::getThumbImage(@$tmp[0], 92, 78,null,true);
		}
        unset($content);                                           

        $allContents = Nine_Function::arrayChunkFixed($allContents, 2);
        
		/**
         * Count all contents
         */
        $count = count($objContent->getAllEnabledContentByCategory($categoryGid,$condition));
        
        /**
         * Assign to view
         */
        
		$this->setPagination($numRowPerPage, $currentPage, $count);
		$this->view->latestNews = $latestNews;
        $this->view->allContents = $allContents;
        $this->view->catName = $catName['name'];
//        echo "<pre>";print_r($latestNews);die;
//echo "<pre>";print_r($allContents);die;
        /**
         * Get current category
         */
        $cat = @reset($objContentCategory->getByColumns(array('content_category_gid=?' => $categoryGid))->toArray());
        $this->view->headTitle($cat['name']);
        $this->view->menuId = "news";
	}
	
	public function detailAction()
	{
		$objContent = new Models_Content();
		$objContentCategory = new Models_ContentCategory();
		
		
		
		$gid = $this->_getParam('id',false);
		if ( false == $gid) {
			$this->_redirectToNotFoundPage();
		}
		
		$news = $objContent->getContentByGid($gid)->toArray();
		
		if (null == $news) {
			$this->_redirectToNotFoundPage();
		}
		
		
		$cat = @reset($objContentCategory->getByColumns(array('content_category_gid=?'=>$news['content_category_gid']))->toArray());
		$name = $objContentCategory->getRootParent(@$news['content_category_gid']);
//		echo "<pre>";print_r($name);die;
		if (null == $cat) {
			$this->_redirectToNotFoundPage();
		}
		if (1 != @$news['content_category_gid']) {
			$news['category_name'] = $cat['name'];
		}
		
		$tmp = explode("||", $news['images']);
		$news['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 239,187,null,true);
		$condition = array(
					'exclude_content_gids'	=>	$gid
					);
		
		if (null != @$news['meta_keywords']) {
        	$this->view->headMetaKeywords($news['meta_keywords']);
        }
        
	 	if (null != @$news['meta_description']) {
        	$this->view->headMetaDescription($news['meta_description']);
        }
        
		$othersNews = $objContent->getAllEnabledContentByCategory($news['content_category_gid'], $condition, array('sorting ASC','content_id DESC')
                                                   );
		
        $i = 0 ;
		$other = array();
		foreach($othersNews as &$item){
			if(2!=$i){
				$item['main_title'] = $item['title'] ;
				$item['title'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['title'])),50);
        	$item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])), 130); 
			if(null!=$item['intro_text']){
        		$item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['intro_text'])), 130); 
        	}
       		 else{
        		$item['intro_text'] = Nine_Function::subStringAtBlank(trim(strip_tags($item['full_text'])), 130); 
        	}       	
        	$item['url'] = Nine_Route::_("content/index/detail/id/{$item['content_gid']}",array('alias' => $item['alias']));
        	$tmp = explode('||', $item['images']);
//			$content['main_image'] = Nine_Function::getThumbImage(@$tmp[0], 244, 191);
			$item['main_image']= Nine_Function::getThumbImage(@$tmp[0], 92, 78,null,true);
        	
        	$other[] = $item;
        	$i++;
			}
			else{
				
			}
        	
        	
        }
        unset($item);
        
	if(1!=$news['content_category_gid'])
		{
			$this->view->headTitle($cat['name']);
			$rootCatParent = $objContentCategory->getRootParent($gid);
        	$this->view->menuId = "content_category_{$rootCatParent['content_category_gid']}";
        	$this->view->name = $name['name'];
		}
		else{
			$this->view->headTitle($news['title']);
			$this->view->name = $news['title'];
			$news['title'] = '';
			$this->view->menuId = $news['title'];
		
			
		}
        
        
        $this->view->news = $news;
        $this->view->others = $other;
//        echo "<pre>";print_r($other);die;
        
//        $this->view->headTitle($news['title']);
//        $this->view->menuId = "news";
	}
	
	
	private function _redirectToNotFoundPage()
	{
	    $this->_redirect("");
	}
}